package com.cdck.safe.dete.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdck.safe.dete.common.result.PageResult;
import com.cdck.safe.dete.common.result.Result;
import com.cdck.safe.dete.model.entity.Notice;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * (Notice)表控制层
 *
 * @author wp
 * @since 2024-02-26 23:02:29
 */
@Api(value = "notice")
@RestController
@RequestMapping("notice")
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService noticeService;


    @ApiOperation(value = "分页列表")
    @GetMapping("/page")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "queryParams"  ,value ="查询条件" )
    })
    public PageResult<Notice> queryByPage(BaseKeywordsQuery queryParams) {
        IPage<Notice> result = noticeService.queryByPage(queryParams);
        return PageResult.success(result);

    }

    @ApiOperation(value = "表单数据")
    @GetMapping("/{id}/form")
    public Result<Notice> getNoticeForm(
           @PathVariable Long id
    ) {
        Notice formData = noticeService.getNoticeFormData(id);
        return Result.success(formData);
    }


    /**
     * 新增数据
     *
     * @param notice 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增")
    @PostMapping
    public Result add(
        @RequestBody @Valid
        Notice notice
        ) {
        boolean result = this.noticeService.saveNotice(notice);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/{id}")
    public Result updateNotice(
            @PathVariable Long id,
            @RequestBody @Validated Notice notice) {
        boolean result = noticeService.updateNotice(id, notice);
        return Result.judge(result);
    }


    @ApiOperation(value = "删除数据")
    @DeleteMapping("/{ids}")
    public Result deleteNotices(
             @PathVariable String ids
    ) {
        boolean result = noticeService.deleteNotices(ids);
        return Result.judge(result);
    }

}

